package com.scvtc.service.teacher.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scvtc.domain.ResResult;
import com.scvtc.domain.entity.teacher.TeacherAttendance;
import com.scvtc.domain.vo.PageVo;
import com.scvtc.domain.vo.teacher.attendance.TeacherAttendanceVo;
import com.scvtc.mapper.teacher.TeacherAttendanceMapper;
import com.scvtc.service.teacher.TeacherAttendanceService;
import com.scvtc.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service("teacherAttendance")
public class TeacherAttendanceServiceImpl extends ServiceImpl<TeacherAttendanceMapper, TeacherAttendance> implements TeacherAttendanceService {
    @Override
    public ResResult<PageVo<List<TeacherAttendanceVo>>> getAllTeacherAttendance(Long current, Long size, TeacherAttendance teacherSocialServiceVo) {
        // 分页查询
//        Page<TeacherAttendanceVo> page = getBaseMapper().getAllTeacherAttendance();
//        // 拷贝bean
//        List<TeacherAttendanceVo> teacherAttendanceVos = BeanCopyUtils.copyBean(page.getRecords(), TeacherAttendanceVo.class);
//        // 统一返回
//        return ResResult.noMsgSuccess(new PageVo<>(teacherAttendanceVos, page.getTotal()));
        Page<TeacherAttendanceVo> page = getBaseMapper().getAllTeacherAttendance(new Page<>(current, size), teacherSocialServiceVo);
        // 拷贝bean
        List<TeacherAttendanceVo> teacherAttendanceVos = BeanCopyUtils.copyBean(page.getRecords(), TeacherAttendanceVo.class);

        return ResResult.noMsgSuccess(new PageVo<>(teacherAttendanceVos, page.getTotal()));
    }

    @Override
    public ResResult<?> addTeacherAttendance(TeacherAttendance teacherAttendance) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
//        TeacherAttendance teacherAttendance = BeanCopyUtils.copyBean(TeacherAttendance, TeacherAttendance.class);
//        teacherAttendance.setTeacherId(TeacherAttendance.getId());

        if (save(teacherAttendance)) {
            return ResResult.success(teacherAttendance.getId());
        }
        return ResResult.fail();
    }

    @Override
    public ResResult<?> deleteAttendance(Long id) {
        if (removeById(id)){
            return ResResult.success();
        }
        return ResResult.fail();
    }

    @Override
    public ResResult<?> updateAttendance(TeacherAttendance teacherAttendance) {
        // 如果考虑后端安全性需要判断教工号是否存在 (代码可参考学生获奖表)
//        TeacherAttendance teacherAttendance = BeanCopyUtils.copyBean(TeacherAttendance, TeacherAttendance.class);
//        teacherAttendance.setTeacherId(TeacherAttendance.getId());

        if (updateById(teacherAttendance)){
            return ResResult.success(teacherAttendance.getId());
        }
        return ResResult.fail();
    }

    @Override
    public ResResult<?> deleteMultipleAttendance(Long[] ids) {
        if (removeByIds(Arrays.asList(ids))){
            return ResResult.success();
        }
        return ResResult.fail();
    }
}
